package klk.shop.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import klk.shop.utilities.KlkConnectionPooling;

/**
 * Dieses Servlet stellt alle Artikel einer Kategorie dar, per
 * default die 15 neuesten Artikel. Hat eine KAtegorie mehr als
 * 15 Artikel, kann zur Navigation die Pagination-Leiste am
 * Seitenende genutzt werden.
 * 
 * @author lueder
 * @version 1.0
 * @since 1.4
 */
public class CategoriesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    /**
     * 
     * @see HttpServlet#HttpServlet()
     */
    public CategoriesServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getContextPath()+"/";
		
		HttpSession session = request.getSession();
		Properties languageLocale = (Properties)session.getAttribute("languageProperty");
		String languageString = languageLocale.getProperty("locale", "en");
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		out.println("<div class='right_box'>");
		out.println("<div class='title'><span class='title_icon'><img src='"+path+"images/bullet5.gif' alt='' title='' /></span>"+languageLocale.getProperty("categories", "Categories")+"</div> ");
		out.println("<ul class='list'>");
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		// Category name aus DB
		String sql = "SELECT id, name_"+languageString+" FROM categories ORDER BY name_"+languageString;
		try {
			conn = KlkConnectionPooling.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				out.println("<li><a href='"+path+"category/"+rs.getString(1)+"'>"+rs.getString(2)+"</a></li>");
			}

		} catch (Exception e) {
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (Exception e) {
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
				}
			if (conn != null)
				try {
					conn.close();
				} catch (Exception e) {
				}
		}
		
		out.println("</ul>");
		out.println(" </div> ");
		out.println("</div><!--end of right content-->");
		//out.println(" <div class='clear'></div>");
		//out.println(" </div><!--end of center content-->");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
